package com.changli02.iterator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IteratorTest {
    @Test
    public void test() {
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add(new Object());
//        System.out.println(c);
        Iterator iterator =c.iterator();
        for (int i = 0; i < c.size(); i++) {
            System.out.println(iterator.next());
        }

    }
    @Test
    public void test2() {
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add(new Object());
        Iterator iterator =c.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //错误方式1  判断与 打印 都跳了指针
//        while (iterator.next()!=null){
//              System.out.println(iterator.next());
//        }
        //错误方式2
//        while(c.iterator().hasNext()){
//            System.out.println(iterator.next());
//        }
    }
    @Test
    public void test3() {
//        增强for  for each
        Collection c = new ArrayList();
        c.add(1);
        c.add("2");
        c.add(new Object());
        for( Object o : c){
            System.out.println(o);
        }


    }
}
